<?php
namespace app\admin\model;

use app\common\model\Worker as WM;

class Worker extends WM
{
    /**
     * [public 根据关键字搜索]
     * @var [type]
     */
    public function scopeWithKeyword($query, $keyword)
    {
        if ( !empty($keyword) ) {
            $query->where('username|job_number|mobile', 'LIKE', '%'.$keyword.'%');
        }
    }

    /**
     * [setPasswdAttr 设置密码]
     * @param [type] $value [description]
     */
    public function setPasswdAttr($value, $data)
    {
        if ( $value !== '' ) {
            return password_hash($value, PASSWORD_DEFAULT);
        } else {
            unset($data['passwd']);
        }
    }

    /**
     * [depart 关联部门]
     * @return [type] [description]
     */
    public function depart()
    {
        return $this->belongsTo( 'WorkerDepartment', 'depart_id', 'id')
        ->field('name');
    }
}
